package com.huashi.dealer.modular.controller.store.goods;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huashi.dealer.commonEntity.Comment;
import com.huashi.dealer.core.page.PageFactory;
import com.huashi.dealer.core.pojo.CommonResult;
import com.huashi.dealer.modular.business.goods.param.StoreCommentListParam;
import com.huashi.dealer.modular.business.goods.param.StoreCommentUpdateParam;
import com.huashi.dealer.modular.business.goods.service.StoreCommentService;
import com.huashi.dealer.modular.business.goods.vo.StoreCommentListVo;
import com.huashi.dealer.modular.business.goods.vo.StoreCommentDetailVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Store端商品评论控制器
 * 对应PHP项目的/store/goods/comment功能
 *
 * @author 谭陈强
 * @since 2025-08-07
 */
@Slf4j
@Tag(name = "Store端商品评论控制器")
@RestController("storeCommentController")
@RequestMapping("/store/goods/comment")
@Validated
public class CommentController {

    @Resource
    private StoreCommentService storeCommentService;

    /**
     * 评论列表
     */
    @GetMapping("/index")
    @Operation(summary = "评论列表")
    public CommonResult<Page<StoreCommentListVo>> getCommentList(StoreCommentListParam param) {
        Page<Comment> page = PageFactory.defaultPage();
        try {
            Page<StoreCommentListVo> commentList = storeCommentService.getCommentList(param, page);
            return CommonResult.data(commentList);
        } catch (Exception e) {
            log.error("获取评论列表失败", e);
            return CommonResult.error("获取评论列表失败：" + e.getMessage());
        }
    }

    /**
     * 评论详情
     */
    @GetMapping("/detail/{commentId}")
    @Operation(summary = "评论详情")
    public CommonResult<StoreCommentDetailVo> getCommentDetail(@PathVariable Long commentId) {
        try {
            StoreCommentDetailVo result = storeCommentService.getCommentDetail(commentId);
            return CommonResult.data(result);
        } catch (Exception e) {
            log.error("获取评论详情失败", e);
            return CommonResult.error("获取评论详情失败：" + e.getMessage());
        }
    }

    /**
     * 更新评论
     */
    @PostMapping("/update")
    @Operation(summary = "更新评论")
    public CommonResult<String> updateComment(@RequestBody StoreCommentUpdateParam param) {
        try {
            storeCommentService.updateComment(param);
            return CommonResult.ok("更新成功");
        } catch (Exception e) {
            log.error("更新评论失败", e);
            return CommonResult.error("更新失败：" + e.getMessage());
        }
    }

    /**
     * 删除评论
     */
    @GetMapping("/delete/{commentId}")
    @Operation(summary = "删除评论")
    public CommonResult<String> deleteComment(@PathVariable Long commentId) {
        try {
            storeCommentService.deleteComment(commentId);
            return CommonResult.ok("删除成功");
        } catch (Exception e) {
            log.error("删除评论失败", e);
            return CommonResult.error("删除失败：" + e.getMessage());
        }
    }
}
